package com.dimples.dd.system.mapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dimples.dd.common.pojo.PageResult;
import com.dimples.dd.mybatis.core.mapper.BaseMapperX;
import com.dimples.dd.mybatis.core.query.LambdaQueryWrapperX;
import com.dimples.dd.system.model.entity.SystemRoleDO;
import com.dimples.dd.system.model.query.RolePageQuery;
import com.dimples.dd.system.model.vo.RolePageVO;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface SystemRoleMapper extends BaseMapperX<SystemRoleDO> {

    /**
     * 角色分页列表
     *
     * @param queryParams 角色查询参数
     * @return {@link Page< RolePageVO >} – 角色分页列表
     */
    default PageResult<SystemRoleDO> getRolePage(RolePageQuery queryParams) {
        return selectPage(queryParams, new LambdaQueryWrapperX<SystemRoleDO>()
                .eqIfPresent(SystemRoleDO::getStatus, queryParams.getStatus())
                .likeIfPresent(SystemRoleDO::getName, queryParams.getKeywords())
                .or()
                .likeIfPresent(SystemRoleDO::getCode, queryParams.getKeywords())
                .orderByAsc(SystemRoleDO::getSort));
    }

    /**
     * 通过code获取id
     *
     * @param roles 凑得集合
     * @return id集合
     */
    default List<Long> listIdsByCodes(List<String> roles) {
        return selectList(new LambdaQueryWrapperX<SystemRoleDO>()
                .inIfPresent(SystemRoleDO::getCode, roles))
                .stream()
                .map(SystemRoleDO::getId)
                .distinct()
                .toList();
    }
}